import { Game_Interpreter } from "@/objects/GameInterpreter.js";
export class Game_CommonEvent {
    constructor(commonEventId) {
        this._commonEventId = commonEventId;
        this.refresh();
    }

    event() {
        return $dataCommonEvents[this._commonEventId];
    }

    list() {
        return this.event().list;
    }

    refresh() {
        if (this.isActive()) {
            if (!this._interpreter) {
                this._interpreter = new Game_Interpreter();
            }
        } else {
            this._interpreter = null;
        }
    }

    isActive() {
        let event = this.event();
        return event?.trigger === 2 && $gameSwitches.value(event?.switchId);
    }

    update() {
        if (this._interpreter) {
            if (!this._interpreter.isRunning()) {
                this._interpreter.setup(this.list());
            }
            this._interpreter.update();
        }
    }

    serialize(){
        return {
            commonEventId:this._commonEventId,
            interpreter:this._interpreter?.serialize()
        }
    }

    deserialize(data){
        this._commonEventId=data.commonEventId;
        if(data.interpreter){
            this._interpreter=new Game_Interpreter();
            this._interpreter.deserialize(data.interpreter);
        }
    }
}